[Scala] Using overloaded, typed methods on a collection

Posted by stephanos on Stack Overflow See other posts from Stack Overflow or by stephanos
Published on 2010-06-03T10:38:13Z Indexed on 2010/06/03 12:14 UTC
Read the original article Hit count: 140

Filed under:

I'm quite new to Scala and struggling with the following:

I have database objects (type of BaseDoc) and value objects (type of BaseVO). Now there are multiple convert methods (all called 'convert') that take an instance of an object and convert it to the other type accordingly. For example:

def convert(doc: ClickDoc): ClickVO = doc match {
    case null => null
    case _ =>
        val result = new ClickVO
        result.x = doc.x
        result.y = doc.y
        result
}

Now I sometimes need to convert a list of objects. How would I do this - I tried:

def convert[D <: MyBaseDoc, V <: BaseVO](docs: List[D]):List[V] = docs match {
    case List() => List()
    case xs => xs.map(doc => convert(doc))
}

Which results in 'overloaded method value convert with alternatives ...'. I tried to add manifest information to it, but couldn't make it work.

I couldn't even create one method for each because it'd say that they have the same parameter type after type erasure (List).

Ideas welcome!

© Stack Overflow or respective owner

Related posts about scala